<template>
	<view class="wrapper" :style="colorStyle">
		<view class="mp-header"
			style="background: linear-gradient(to bottom, rgb(247, 235, 234) 0%, rgb(247, 247, 247) 100%;">
			<view class="sys-head" :style="{ height: sysHeight }"></view>
			<view class="serch-box" style="height: 43px; line-height: 43px;text-align: center;position: relative;">
				<!-- <view class="backBtn" @click="pageBack" style="margin-top: 15rpx;">
					<image src="/static/svg/back.svg" style="width: 24px;height: 24px;"></image>
				</view> -->

				<view class="name">我的设计</view>
			</view>
		</view>
		<view class='productList'>
			<view class='search acea-row row-between-wrapper'
				style="background: linear-gradient(to bottom, rgb(247, 247, 247) 0%, rgb(255, 255, 255) 100% !important;">
				<view class='input acea-row row-between-wrapper' style="border: 1px solid #000;"><text
						class='iconfont icon-sousuo'></text>
					<input :placeholder='$t(`搜索商品名称`)' placeholder-class='placeholder' confirm-type='search'
						name="search" :value='where.keyword' @confirm="searchSubmit"></input>
				</view>
			</view>
			<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scroll="scroll"
				@scrolltolower="scrolltolower">
				<view class='list acea-row row-between-wrapper' :class='is_switch==true?"":"on"'>
					<view class="box" v-for="(item,index) in productList" :key="index">
						<view class='item' :class='is_switch==true?"":"on"' hover-class='none' @click="godDetail(item)">
							<view class='pictrue' :class='is_switch==true?"":"on"'
								style="width: 140rpx;height: 140rpx;">
								<image :src='item.image' :class='is_switch==true?"":"on"'></image>
							</view>
							<view
								style="font-size: 16px; font-weight: 700;line-height: 23px;margin-top: 5px;overflow: hidden;padding-left: 15rpx;padding-right: 15rpx;margin-bottom: 20rpx;">
								<view class="line1">{{item.product.storeInfo.store_name}}</view>
								<view class="line2">￥{{item.product.storeInfo.price}}</view>
							</view>
						</view>
						<view class="footerBox">
							<view class="cItem" @click="delItem(item)">
								<view class="icon">
									<image src="/static/svg/del.svg" style="width: 30rpx;height: 30rpx;"></image>
								</view>
								<view class="name">
									删除
								</view>
							</view>
							<view class="cItem" @click="reSet(item)">
								<view class="icon">
									<image src="/static/svg/edit.svg" style="width: 30rpx;height: 30rpx;"></image>
								</view>
								<view class="name" style="color: #ff3d61;">
									重新编辑
								</view>
							</view>
							<view class="cItem" @click="addCart(item)">
								<view class="icon">
									<image src="/static/svg/cart.svg" style="width: 30rpx;height: 30rpx;"></image>
								</view>
								<view class="name" style="color: #ff3d61;">
									加入购物车
								</view>
							</view>
						</view>
					</view>

					<view class='loadingicon acea-row row-center-wrapper' v-if='productList.length > 0'>
						<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
					</view>
				</view>
				
				<view class='noCommodity' v-if="productList.length==0 && where.page > 1">
					<view class='emptyBox'>
						<image :src="imgHost + '/statics/images/no-thing.png'"></image>
						<view class="tips">{{$t(`暂无设计商品，去看点别的吧`)}}</view>
					</view>
				</view>
			</scroll-view>

			<view style="height: 100rpx;"></view>
		</view>
		
		<!-- #ifndef MP -->
		<home></home>
		<!-- #endif -->
		<view v-if="scrollTopShow" class="back-top" @click="goTop">
			<text class="iconfont icon-xiangshang"></text>
		</view>

		<pageFooter></pageFooter>
	</view>
</template>

<script>
	let sysHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
	import home from '@/components/home';
	import {
		getDesign,
		getProductHot,
		postCartAdd,
		delDesign
	} from '@/api/store.js';
	import recommend from '@/components/recommend';
	import {
		mapGetters
	} from "vuex";
	import {
		goShopDetail
	} from '@/libs/order.js'
	import {
		HTTP_REQUEST_URL
	} from '@/config/app';
	import colors from '@/mixins/color.js';
	import pageFooter from '@/components/pageFooter/index.vue'
	export default {
		computed: mapGetters(['uid']),
		components: {
			recommend,
			home,
			pageFooter
		},
		mixins: [colors],
		data() {
			return {
				imgHost: HTTP_REQUEST_URL,
				productList: [],
				is_switch: false,
				where: {
					sid: 0,
					keyword: '',
					priceOrder: '',
					salesOrder: '',
					news: 0,
					page: 1,
					limit: 20,
					cid: 0,
				},
				price: 0,
				stock: 0,
				nows: false,
				loadend: false,
				loading: false,
				loadTitle: this.$t(`加载更多`),
				title: '',
				hostProduct: [],
				hotPage: 1,
				hotLimit: 10,
				hotScroll: false,
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				scrollTopShow: false,
				sysHeight: sysHeight
			};
		},
		onLoad: function(options) {
			this.where.cid = options.cid || 0;
			this.where.coupon_category_id = options.coupon_category_id || '';
			this.$set(this.where, 'sid', options.sid || 0);
			this.title = options.title || '';
			this.$set(this.where, 'keyword', options.searchValue || '');
			this.$set(this.where, 'productId', options.productId || '');
			// this.get_product_list();
		},
		onShow() {
			this.where = {
				sid: 0,
				keyword: '',
				priceOrder: '',
				salesOrder: '',
				news: 0,
				page: 1,
				limit: 20,
				cid: 0,
			}
			this.loadend = false;
			this.loading = false;
			this.productList = [];
			this.get_product_list();
		},
		methods: {
			delItem(item) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确定要删除设计吗？',
					success: function(res) {
						if (res.confirm) {
							// 用户点击确定，执行删除操作
							that.delNow(item)
						} else if (res.cancel) {
							// 用户点击取消，什么都不做
							console.log('已取消');
						}
					}
				});
			},
			delNow(item) {
				let that = this;
				delDesign({
					design_id: item.id
				}).then(res => {
					console.log('删除成功')
					let time = 1500;
					uni.showToast({
						title: '删除成功',
						icon: 'success',
						duration: time
					});
				
					setTimeout(() => {
						that.loadend = false;
						that.loading = false;
						that.productList = [];
						that.get_product_list()
					}, time)
				})
			},
			reSet(item) {
				console.log('重新编辑')
				uni.navigateTo({
					url: '/pages/design/design?isEdit=1&id=' + item.id + '&token=' + this.$store.state.app.token
				})
			},
			pageBack() {
				uni.navigateBack();
			},
			scroll(e) {
				this.scrollTopShow = e.detail.scrollTop > 150
				this.old.scrollTop = e.detail.scrollTop
			},
			goTop(e) {
				// 解决view层不同步的问题
				this.scrollTop = this.old.scrollTop
				this.$nextTick(() => {
					this.scrollTop = 0
				});
			},

			// 加入购物车
			addCart(item) {
				let that = this;
				console.log('item', item)
				let cartNum = 0;
				let isNew = 0;
				let uniqueId = '';
				let virtual_type = '';
				for (let i = 0; i < item.extra; i++) {
					let thatItem = item.extra[i];
					if (thatItem.name == 'cartNum') {
						cartNum = thatItem.value;
					}

					if (thatItem.name == 'cartNum') {
						cartNum = thatItem.value;
					}

					if (thatItem.name == 'new') {
						isNew = thatItem.value;
					}

					if (thatItem.name == 'uniqueId') {
						uniqueId = thatItem.value;
					}

					if (thatItem.name == 'virtual_type') {
						virtual_type = thatItem.value;
					}
				}
				let q = {
					productId: item.product_id,
					cartNum: cartNum,
					new: isNew,
					uniqueId: uniqueId,
					virtual_type: virtual_type,
					designId: item.id
				};

				postCartAdd(q)
					.then(function(res) {
						that.$util.Tips({
							title: that.$t(`添加成功`),
							success: () => {
								setTimeout(() => {
									uni.switchTab({
										url: '/pages/order_addcart/order_addcart'
									})
								}, 1500)

							}
						});

					})
					.catch((err) => {
						that.isOpen = false;
						return that.$util.Tips({
							title: err
						});
					});
			},
			// 去详情页
			godDetail(item) {
				goShopDetail(item, this.uid).then(res => {
					uni.navigateTo({
						url: `/pages/goods_details/index?id=${item.id}`
					})
				})
			},
			Changswitch: function() {
				let that = this;
				that.is_switch = !that.is_switch
			},
			searchSubmit: function(e) {
				let that = this;
				that.$set(that.where, 'keyword', e.detail.value);
				that.loadend = false;
				that.$set(that.where, 'page', 1)
				this.get_product_list(true);
			},
			/**
			 * 获取我的推荐
			 */
			get_host_product: function() {
				let that = this;
				if (that.hotScroll) return
				getProductHot(
					that.hotPage,
					that.hotLimit,
				).then(res => {
					that.hotPage++
					that.hotScroll = res.data.length < that.hotLimit
					that.hostProduct = that.hostProduct.concat(res.data)
					// that.$set(that, 'hostProduct', res.data)
				});
			},
			//点击事件处理
			set_where: function(e) {
				switch (e) {
					case 1:
						// #ifdef H5
						return history.back();
						// #endif
						// #ifndef H5
						return uni.navigateBack({
							delta: 1,
						})
						// #endif
						break;
					case 2:
						if (this.price == 0) this.price = 1;
						else if (this.price == 1) this.price = 2;
						else if (this.price == 2) this.price = 0;
						this.stock = 0;
						break;
					case 3:
						if (this.stock == 0) this.stock = 1;
						else if (this.stock == 1) this.stock = 2;
						else if (this.stock == 2) this.stock = 0;
						this.price = 0
						break;
					case 4:
						this.nows = !this.nows;
						break;
				}
				this.loadend = false;
				this.$set(this.where, 'page', 1);
				this.get_product_list(true);
			},
			//设置where条件
			setWhere: function() {
				if (this.price == 0) this.where.priceOrder = '';
				else if (this.price == 1) this.where.priceOrder = 'asc';
				else if (this.price == 2) this.where.priceOrder = 'desc';
				if (this.stock == 0) this.where.salesOrder = '';
				else if (this.stock == 1) this.where.salesOrder = 'asc';
				else if (this.stock == 2) this.where.salesOrder = 'desc';
				this.where.news = this.nows ? 1 : 0;
			},
			//查找产品
			get_product_list: function(isPage) {
				let that = this;
				that.setWhere();
				if (that.loadend) return;
				if (that.loading) return;
				if (isPage === true) that.$set(that, 'productList', []);
				that.loading = true;
				that.loadTitle = '';
				getDesign(that.where).then(res => {
					let list = res.data.result;
					let productList = that.$util.SplitArray(list, that.productList);
					let loadend = list.length < that.where.limit;

					for (let i = 0; i < productList.length; i++) {
						let item = productList[i];
						console.log('item', item)
						item.image = ''
						for (let j = 0; j < item.pic.length; j++) {
							if (item.pic[j].title === '定制图') {
								item.image = item.pic[j].url;
							}
						}
					}
					that.loadend = loadend;
					that.loading = false;
					that.loadTitle = loadend ? that.$t(`没有更多内容啦~`) : that.$t(`加载更多`);
					that.$set(that, 'productList', productList);
					that.$set(that.where, 'page', that.where.page + 1);
					if (!that.productList.length) this.get_host_product();
				}).catch(err => {
					that.loading = false;
					that.loadTitle = that.$t(`加载更多`);
				});
			},
			scrolltolower() {
				if (this.productList.length > 0) {
					this.get_product_list();
					uni.$emit('scroll');
				} else {
					this.get_host_product();
					uni.$emit('scroll');
				}
			}
		},
		onPullDownRefresh() {},
		onReachBottom() {},
		// 滚动监听
		onPageScroll(e) {
			// 传入scrollTop值并触发所有easy-loadimage组件下的滚动监听事件
			uni.$emit('scroll');
		},
	}
</script>

<style scoped lang="scss">
	.scroll-Y {
		margin-top: 0;
		height: calc(100vh - 43rpx);
	}

	.wrapper {
		position: relative;
		max-height: 100vh;
		overflow: hidden;

		.back-top {
			position: absolute;
			right: 40rpx;
			bottom: 60rpx;
			width: 60rpx;
			height: 60rpx;
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;
			border: 1rpx solid #ccc;
			background-color: #fff;

			.icon-xiangshang {
				color: #ccc;
				font-weight: bold;
			}
		}
	}

	.productList .search {
		width: 100%;
		height: 86rpx;
		padding-left: 23rpx;
		box-sizing: border-box;
		// background-color: linear-gradient(to right, rgba(247, 235, 233, 1) 0%, rgba(248, 247, 244, 1) 100%) !important;
		// position: fixed;
		// left: 0;
		// top: 0;
		// z-index: 9;
	}

	.productList .search .input {
		width: 710rpx;
		height: 60rpx;
		background-color: #fff;
		border-radius: 50rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.productList .search .input input {
		width: 620rpx;
		height: 100%;
		font-size: 26rpx;
	}

	.productList .search .input .placeholder {
		color: #999;
	}

	.productList .search .input .iconfont {
		font-size: 35rpx;
		color: #555;
	}

	.productList .search .icon-pailie,
	.productList .search .icon-tupianpailie {
		color: #fff;
		width: 62rpx;
		font-size: 40rpx;
		height: 86rpx;
		line-height: 86rpx;
	}

	.productList .nav {
		height: 86rpx;
		color: #454545;
		// position: fixed;
		// left: 0;
		width: 100%;
		font-size: 28rpx;
		background-color: #fff;
		margin-top: 0;
		// top: 0;
		// z-index: 9;
	}

	.productList .nav .item {
		width: 25%;
		text-align: center;
	}

	.productList .nav .item.font-color {
		font-weight: bold;
	}

	.productList .nav .item image {
		width: 15rpx;
		height: 19rpx;
		margin-left: 10rpx;
	}

	.productList .list {
		padding: 0 20rpx 30rpx 20rpx;
		margin-top: 0;
		margin-bottom: 100rpx;
	}

	.productList .list.on {
		// background-color: #fff;
		border-top: 1px solid #f6f6f6;
	}

	.productList .list .item {
		width: 200rpx;
		margin-top: 20rpx;
		background-color: #fff;
		border-radius: 20rpx;
	}

	.productList .list .item.on {
		width: 100%;
		display: flex;
		border-bottom: 1rpx solid #f6f6f6;
		padding: 30rpx 0;
		margin: 0;
		margin-bottom: 10rpx;
	}

	.productList .list .footerBox {
		// border-top: 1px solid rgba(234,234,234,1);
		display: flex;
		background-color: #fff;
		padding: 20rpx;
		justify-content: space-between;

		.cItem {
			display: flex;

			image {
				position: relative;
				top: 2px;
				margin-right: 10rpx;
			}
		}
	}

	.productList .list .item .pictrue {
		position: relative;
		width: 100%;
		height: 200rpx;
		margin-left: 20rpx;
		margin-right: 30rpx;
	}

	.productList .list .item .name {
		line-height: 42rpx;
		height: 84rpx;
	}

	.productList .list .item .pictrue.on {
		width: 100rpx;
		height: 100rpx;
	}

	.productList .list .item .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 20rpx 20rpx 0 0;
	}

	.productList .list .item .pictrue image.on {
		border-radius: 6rpx;
	}

	.productList .list .item .text {
		padding: 20rpx 17rpx 26rpx 17rpx;
		font-size: 30rpx;
		color: #222;
	}

	.productList .list .item .text.on {
		width: 508rpx;
		padding: 0 0 0 22rpx;
	}

	.productList .list .item .text .money {
		font-size: 26rpx;
		font-weight: bold;
		margin-top: 8rpx;
	}

	.productList .list .item .text .money.on {
		margin-top: 50rpx;
	}

	.productList .list .item .text .money .num {
		font-size: 34rpx;
	}

	.productList .list .item .text .vip {
		font-size: 22rpx;
		color: #aaa;
		margin-top: 7rpx;
	}

	.productList .list .item .text .vip.on {
		margin-top: 12rpx;
	}

	.productList .list .item .text .vip .vip-money {
		font-size: 24rpx;
		color: #282828;
		font-weight: bold;
		display: flex;
		align-items: center;
	}

	.productList .list .item .text .vip .vip-money image {
		width: 64rpx;
		height: 26rpx;
		margin-left: 4rpx;
	}

	.noCommodity {
		background-color: #fff;
		padding-bottom: 30rpx;

		.emptyBox {
			text-align: center;
			padding-top: 20rpx;

			.tips {
				color: #aaa;
				font-size: 26rpx;
			}

			image {
				width: 414rpx;
				height: 304rpx;
			}
		}
	}

	.line2 {
		margin-top: 20rpx;
		color: #999;
	}

	.box {
		width: 100%;
		background-color: #fff;
		margin-bottom: 10rpx;
	}
</style>